<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/21
 * Time: 下午12:06
 */

class AdminBaseController extends BaseController {
    public $layout = 'admin', $game, $platform;

    // 前置函数
    public function beforeAction($action){
        $this->game = $this->R('g');
        $this->platform = '';

        if(empty($this->game)){
            $this->redirect('/home/?r=site/game');
        }

        if(parent::beforeAction($action)){
            $games = PubBase::getGame(true);
            $platforms = PubBase::getPlatform(true);

            $this->out['beforeAction']['Nav'] = array(
                'Options' => $games,
                'Value'   => $this->game
            );

            $menu = PubBase::getMenu($this->game, $this->platform);
            
            if(empty($menu)){
                //$this->redirect('/home/?r=site/game');
            }
            $this->out['beforeAction']['Menu'] = $menu;
            $this->out['beforeAction']['Current']['GameSrc'] = $this->game;
            $this->out['beforeAction']['Current']['Platform'] = $this->platform;
            $this->out['beforeAction']['Current']['GameName'] = $games[$this->game];
            $this->out['beforeAction']['Current']['PlatformName'] = $platforms[$this->platform];

            return true;
        }else{
            return false;
        }
    }

    protected function getJumpUrl($url){
        $params = array(
            'm' => $this->R('m'),
            'g' => $this->game
        );
        return $url . ((strpos($url, '?')) ? '&' : '?') . http_build_query($params);
    }
    
    /**
     * 记住上次筛选的条件和过滤不合法的查询
     * 登录之后第一次打开页面，因为第一次进入页面ana_s还没有赋值，session又没有值，则以当前最初默认选择为条件，此时这里如若不选择筛选条件，则无需用session记住
     * 如若选择筛选条件，则用session记住，且用当前筛选条件进行搜索
     * 当再次进入另外一个新页面，页面新进入ana_s还没有赋值，此时这里已经不是第一次进入页面，页面已经有记住上次筛选的条件在session，所以此时这里用上次记住的条件作为搜索条件
     * 
     * @param unknown $params
     * @return unknown
     */
    public function indexCommon($params){
    	$ana_s = PubRequest::R('ana_s');
    	if(isset($ana_s)){
    		$params_s = array();
    		$params_s['special_id'] = $params['special_id'];
    		$params_s['os'] = $params['os'];
    		$params_s['server'] = $params['server'];
    		$params_s['time_start'] = $params['time_start'];
    		$params_s['time_end'] = $params['time_end'];
    		PubAuthSession::set('ana_s',$params_s);
    	}else{   		
    		$params_s = PubAuthSession::get('ana_s');
    		if(isset($params_s)){
    			$params['special_id'] = $params_s['special_id'];
    			$params['os'] = $params_s['os'];
    			$params['server'] = $params_s['server'];
    			//当页面不存在time_start、time_end的时候会使session的time_start和time_end变为null，导致在利用所记住session作为查询条件时出现错误
    			
    			if($params_s['time_start'] != ''){   				
    				$params['time_start'] = $params_s['time_start'];
    			}
    			if($params_s['time_end'] != ''){
    				$params['time_end'] = $params_s['time_end'];
    			}

    		}
    	}

    	$params['ana_s'] = 2;
    	
    	$options = PubAuthSession::get('os');
    	$os_array = array();
    	foreach ($options as $key=>$val){
    		$os_array[] = $key;
    	}
    	$user = AccAuth::me();
    	if($user['IsViewAll'] == 1){
    		$os_array['ALL'] = 'ALL';
    	}
    	if(!in_array($params['os'], $os_array)){
    		unset($params['os']);
    	}
    	
    	return $params;
    }
    
    public function logCommon($params){
        $log_s = PubRequest::R('log_s');
        if(isset($log_s)){
        	$params_s = array();
        	$params_s['special_id'] = $params['special_id'];
        	$params_s['os'] = $params['os'];
        	$params_s['server'] = $params['server'];
        	$params_s['port'] = $params['port'];
        	$params_s['time'] = $params['time'];
        	$params_s['time_start'] = $params['time_start'];
        	$params_s['time_end'] = $params['time_end'];
        	PubAuthSession::set('params_s',$params_s);
        }else{
        	//echo '第一次';
        	$params_s = PubAuthSession::get('params_s'); 
        	if(isset($params_s)){
        		$params['special_id'] = $params_s['special_id'];
        		$params['os'] = $params_s['os'];
        		$params['server'] = $params_s['server'];
        		$params['port'] = $params_s['port'];
        		$params['time'] = $params_s['time'];
        		$params['time_start'] = $params_s['time_start'];
        		$params['time_end'] = $params_s['time_end'];
        	}      
        	
        }
        $params['log_s'] = 1;
        
        $options = PubAuthSession::get('os');
        $os_array = array();
        foreach ($options as $key=>$val){
        	$os_array[] = $key;
        }
        $user = AccAuth::me();
        if($user['IsViewAll'] == 1){
        	$os_array['ALL'] = 'ALL';
        }
        if(!in_array($params['os'], $os_array) && $params['os'] != 'ALL' ){
        	unset($params['os']);
        }
    	 
    	return $params;
    }
    
} 
